package com.jie.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 曾豪杰
 * @Date: 2022/09/10/12:47
 * @Description:
 */
@ControllerAdvice(annotations = {RestController.class, Controller.class})
@Slf4j
public class ControllerExceptionHandler {



//    @ExceptionHandler(CustomerException.class)
//    public ModelAndView customerHandler(CustomerException e){
//        log.error("exception:{}",e.getMessage());
//        ModelAndView modelAndView=new ModelAndView();
//        modelAndView.addObject("exception",e);
//        modelAndView.setViewName("/error/404");
//        return modelAndView;
//    }

    @ExceptionHandler(Exception.class)
    public ModelAndView exceptionHandler(HttpServletRequest request, Exception ee) throws Exception {
        log.error("request url:{},exception:{}",request.getRequestURI(),ee.getMessage());
        //
        if(AnnotationUtils.findAnnotation(ee.getClass(), ResponseStatus.class)!=null){
                throw ee;
        }
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("url",request.getRequestURI());
        modelAndView.addObject("exception",ee);
        modelAndView.setViewName("/error/error");
        return modelAndView;
    }


}
